﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class YaxMachineController : Controller
    {
        private YaxMachineInfo yaxinfo = new YaxMachineInfo(true);
        public ActionResult YaxMachineIndex()
        {
            ViewData["MacTypeName"] = GetMacTypeNameIndex();
            return View();
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetYaxMachineList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string mactypename = "";
                string[] param = pvm.query.Split(',');
                if (param[1].ToString() != "-1")
                {
                    mactypename += " and MacTypeID='" + param[1].ToString() + "' ";
                }
                else
                {
                    mactypename += " and 1=1 ";
                }
                query = " and NodeName like '%" + param[0] + "%'" + mactypename;
            }
            IList<YaxMachineInfo> yaxlist = yaxinfo.SelYaxMachineInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = yaxinfo._YaxMachinecount;
            List<string> v;
            foreach (YaxMachineInfo item in yaxlist)
            {
                #region
                v = new List<string>();
                v.Add(item.MacID.ToString());
                v.Add(item.NodeName);
                v.Add(item.MacNo.ToString());
                v.Add(item.MacTypeName);
                v.Add(item.MacDescribe);
                v.Add(item.CommStyle_);
                v.Add(item.PortNo.ToString());
                v.Add(item.BaudRate.ToString());
                v.Add(item.IPAddress);
                v.Add(item.GetwayIP);
                v.Add(item.SubIPAddr);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.MacID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteYaxMachine(string ID)
        {
            int result = 0;
            result = yaxinfo.DeleteYaxMachine(ID);
            return Json(result);
        }

        public ActionResult AddYaxMachine()
        {
            ViewData["NodeName"] = GetNodeName();
            ViewData["MacTypeName"] = GetMacTypeName();
            ViewData["CommStyle"] = GetCommStyle();
            return View();
        }

        /// <summary>
        /// 获取设备组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetNodeName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = yaxinfo.GetNodeName();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["NodeID"]), ds.Tables[0].Rows[i]["Nodename"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取设备类型列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMacTypeNameIndex()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = yaxinfo.GetMacTypeName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["MacTypeID"]), ds.Tables[0].Rows[i]["MacTypeName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取设备类型列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMacTypeName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = yaxinfo.GetMacTypeName();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["MacTypeID"]), ds.Tables[0].Rows[i]["MacTypeName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取通讯方式列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetCommStyle()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(0, "串口");
            dic.Add(1, "TCP/IP");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 保存设备（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveYaxMachine(YaxMachineVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                YaxMachineInfo yax = new YaxMachineInfo()
                {
                    MacNo = model.MacNoAdd,
                    NodeID = Convert.ToInt32(model.NodeName),
                    MacType = Convert.ToInt32(model.MacTypeName),
                    CommStyle = model.CommStyle,
                    PortNo = Convert.ToInt32(model.PortNo),
                    BaudRate = Convert.ToInt32(model.BaudRate),
                    IPAddress = model.IPAddress,
                    GetwayIP = model.GetwayIP,
                    SubIPAddr = model.SubIPAddr,
                    MacDescribe = model.MacDescribe
                };
                result = yaxinfo.SaveYaxMachine(yax);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditYaxMachine(string id)
        {
            ViewData["NodeName"] = GetNodeName();
            ViewData["MacTypeName"] = GetMacTypeName();
            ViewData["CommStyle"] = GetCommStyle();
            YaxMachineVM vm = new YaxMachineVM();
            YaxMachineInfo info = new YaxMachineInfo();
            yaxinfo = info.GetYaxMachineInfoOne(id);
            vm.AddEdit = "Edit";
            vm.OldMacNo = yaxinfo.MacNo;
            vm.MacID = yaxinfo.MacID;
            vm.NodeID = yaxinfo.NodeID;
            vm.MacNo = yaxinfo.MacNo;
            vm.MacDescribe = yaxinfo.MacDescribe;
            vm.MacType = yaxinfo.MacType;
            vm.CommStyle = yaxinfo.CommStyle;
            vm.PortNo = yaxinfo.PortNo;
            vm.BaudRate = yaxinfo.BaudRate.ToString();
            vm.IPAddress = yaxinfo.IPAddress;
            vm.GetwayIP = yaxinfo.GetwayIP;
            vm.SubIPAddr = yaxinfo.SubIPAddr;
            Mapper.DynamicMap(yaxinfo, vm);
            return View(vm);
        }

        public ActionResult SaveYaxMachineEdit(YaxMachineVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                YaxMachineInfo yax = new YaxMachineInfo()
                {
                    MacID = model.MacID,
                    MacNo = model.MacNo,
                    NodeID = Convert.ToInt32(model.NodeID),
                    MacType = Convert.ToInt32(model.MacType),
                    CommStyle = model.CommStyle,
                    PortNo = Convert.ToInt32(model.PortNo),
                    BaudRate = Convert.ToInt32(model.BaudRate),
                    IPAddress = model.IPAddress,
                    GetwayIP = model.GetwayIP,
                    SubIPAddr = model.SubIPAddr,
                    MacDescribe = model.MacDescribe
                };
                result = yaxinfo.SaveYaxMachineEdit(yax);
            }
            return Json(result);
        }

        /// <summary>
        /// 设备编号验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckMacNo(int MacNo, int OldMacNo, string AddEdit)
        {
            bool istongguo = yaxinfo.CheckMacNo(MacNo, OldMacNo, AddEdit);
            return Json(istongguo);
        }

        /// <summary>
        /// 设备编号验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckMacNoAdd(int MacNoAdd)
        {
            bool istongguo = yaxinfo.CheckMacNoAdd(MacNoAdd);
            return Json(istongguo);
        }

        /// <summary>
        /// 检查是否为系统保留项
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult SelYaxMachineEdit(string ID)
        {
            int result = 0;
            result = yaxinfo.SelYaxMachineEdit(ID);
            return Json(result);
        }
    }
}
